package com.qf.android12_1_touchtest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2016/12/1.
 */
public class TouchView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
    private Bitmap bitmap;
    private Matrix matrix;

    private float lastDis;
    private float lastDegress;
    private GestureDetector detector;

    private boolean isDouble;

    public TouchView(Context context) {
        this(context,null);
    }

    public TouchView(Context context, AttributeSet attrs) {
       this(context,attrs,0);
    }

    public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
        matrix = new Matrix();//矩阵变换，平移旋转缩放倾斜
        //set:重置当前矩阵，并设置新的，post:在当前矩阵的基础上再去添加一个矩阵，pre:在当前矩阵变化前插入一个变化矩阵
        detector = new GestureDetector(context,this);
        detector.setOnDoubleTapListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            isDouble=true;
            float x1 = event.getX(0);
            float x2 = event.getX(1);
            float y1 = event.getY(0);
            float y2 = event.getY(1);
            float cx = (x1 + x2) / 2;
            float cy = (y1 + y2) / 2;
            float dis = (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
            float degress = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1));
            switch (event.getActionMasked()) {//多指操作一定要用这个
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    lastDis = dis;
                    lastDegress = degress;
                    break;
                case MotionEvent.ACTION_MOVE:
                    matrix.postScale(dis / lastDis,dis / lastDis ,cx,cy);
                    matrix.postRotate(degress-lastDegress,cx,cy);
                    lastDis = dis;
                    lastDegress = degress;
                    break;
            }
        }else if (event.getPointerCount()==1){
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                isDouble=false;
            }
            if (!isDouble) {
                return detector.onTouchEvent(event);
            }
        }
        invalidate();
        return true;
        //返回true，不可以有onclickListener了
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
        //和down的延迟大概是150ms
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        //单击事件
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        //滚动事件，滑动前后的MotionEvent,v=水平位移，v1=垂直位移（前一个减后一个，是负数！！！）
        matrix.postTranslate(-v,-v1);
        invalidate();
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
        //长按事件，与单击事件互斥
    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        //抛动事件，v=水平速度，v1=垂直速度
        return false;
    }


//双击监听3个回调方法
    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        matrix.postScale(1.3f,1.3f,motionEvent.getX(),motionEvent.getY());
        invalidate();
        return false;
    }
}
